package com.shopping.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.common.entity.Good;
import com.shopping.common.result.PageResult;
import com.shopping.common.result.Result;
import com.shopping.es.service.EsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/es")
@Log4j2
public class EsController {

    @Autowired
    EsService esService;

    @Autowired
    HttpServletRequest request;

    /**
     * 同步商品信息
     * @param list
     */
    @PostMapping("/addGoodToEs")
    public int addGoodToEs(@RequestBody List<Good> list){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(list));
        esService.addGood(list);
        return 1;
    }

    /**
     * 通过商品名查找商品
     * @return
     */
    @PostMapping("/findGoodByName/{name}")
    public Result<PageResult<Good>> findGoodByName(@PathVariable String name){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), name);

        Result<PageResult<Good>> result = esService.findGoodByName(name);
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);
        return result;
    }
}
